﻿using System;
using System.Collections.Generic;
using System.IO;
using Autodesk.Revit.DB;
using FlexCell;
using YArchitech.Controls;
using HyMaterialStatistic;

namespace ElectricMaterialStatistic
{
	internal class ElectricMaterialList
	{
        public ElectricMaterialList(Document doc, List<Dictionary<int, SatatisticDataInfo>> lstDicElement, string gridName, GridSetInfo gridSetInfo, ref FlexCell.Grid grid)
		{
			this.m_doc = doc;
			this.m_grid = grid;
			this.m_lstDicElems = lstDicElement;
			this.m_strGridName = gridName;
			this.m_gridSetInfo = gridSetInfo;
		}

		public bool GetStatisticGrid()
		{
			if (this.m_lstDicElems.Count <= 0)
			{
				return false;
			}
			this.GetStatisticData();
			this.m_grid.Locked = false;
			if (!this.m_gridSetInfo.TitleDown)
			{
				if (!this.SetDataToGridUp())
				{
					return false;
				}
			}
			else if (!this.SetDataToGridDown())
			{
				return false;
			}
			return true;
		}

		private void GetStatisticData()
		{
			if (this.m_doc == null)
			{
				return;
			}
			int index;
			if (this.m_gridSetInfo.Legend)
			{
				index = 5;
			}
			else
			{
				index = 4;
			}
			int num = 1;
			foreach (Dictionary<int, SatatisticDataInfo> dictionary in this.m_lstDicElems)
			{
				if (dictionary.Count > 0)
				{
					Dictionary<int, ElectricMaterialList.GridParameter> dictionary2 = new Dictionary<int, ElectricMaterialList.GridParameter>();
					foreach (KeyValuePair<int, SatatisticDataInfo> keyValuePair in dictionary)
					{
						ElementId elementId = new ElementId(keyValuePair.Key);
						this.m_doc.GetElement(elementId);
						int key = -1;
						if (this.ElemExsit(keyValuePair.Value.Name, keyValuePair.Value.LstFilter, dictionary2, out key))
						{
							string text = (Convert.ToDouble(dictionary2[key].LstParm[index]) + keyValuePair.Value.Amount).ToString("0.00");
							if (text.IndexOf('.') != -1)
							{
								text = text.TrimEnd(new char[]
								{
									'0'
								});
								text = text.Trim(new char[]
								{
									'.'
								});
							}
							dictionary2[key].LstParm[index] = text.ToString();
						}
						else
						{
							List<string> list = new List<string>();
							int num2 = 0;
							list.Insert(num2, num.ToString());
							if (this.m_gridSetInfo.Legend)
							{
								list.Insert(++num2, keyValuePair.Value.FamilyName);
							}
							list.Insert(++num2, keyValuePair.Value.Name);
							list.Insert(++num2, "");
							list.Insert(++num2, keyValuePair.Value.Unit);
							string text2 = keyValuePair.Value.Amount.ToString("0.00");
							if (text2.IndexOf('.') != -1)
							{
								text2 = text2.TrimEnd(new char[]
								{
									'0'
								});
								text2 = text2.Trim(new char[]
								{
									'.'
								});
							}
							list.Insert(++num2, text2);
							list.Insert(num2 + 1, "");
							dictionary2.Add(num, new ElectricMaterialList.GridParameter(list, keyValuePair.Value.LstElemParm, keyValuePair.Value.LstFilter, keyValuePair.Value.Unit));
							num++;
						}
					}
					this.m_lstDicData.Add(dictionary2);
				}
			}
		}

		private bool ElemExsit(string name, List<string> lstFilter, Dictionary<int, ElectricMaterialList.GridParameter> dicData, out int key)
		{
			int index;
			if (this.m_gridSetInfo.Legend)
			{
				index = 2;
			}
			else
			{
				index = 1;
			}
			foreach (int num in dicData.Keys)
			{
				List<string> lstParm = dicData[num].LstParm;
				if (name == lstParm[index] && this.SameFilter(dicData[num].LstFilter, lstFilter))
				{
					key = num;
					return true;
				}
			}
			key = -1;
			return false;
		}

		private bool SameFilter(List<string> lstCurFilter, List<string> lstFilter)
		{
			if (lstCurFilter.Count != lstFilter.Count)
			{
				return false;
			}
			for (int i = 0; i < lstCurFilter.Count; i++)
			{
				if (lstCurFilter[i] != lstFilter[i])
				{
					return false;
				}
			}
			return true;
		}

		private bool SetDataToGridUp()
		{
			if (this.m_lstDicData.Count <= 0 || this.m_grid == null)
			{
				YJKMessageBox.Show("没有统计数据");
				return false;
			}
			int num = -1;
			int num2 = 0;
			int col = 0;
			int col2 = 0;
			if (this.m_gridSetInfo.Legend)
			{
				num = 1;
				num2 = 5;
				col = 4;
				col2 = 3;
			}
			else
			{
				num2 = 4;
				col = 3;
				col2 = 2;
			}
			this.m_grid.Cell(1, 1).Text = this.m_strGridName;
			this.m_grid.Cell(1, 1).FontSize = this.m_gridSetInfo.TitleFontSize;
			int num3 = 3;
			this.m_grid.Range(num3 + 1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).DeleteByRow();
			int num4 = 0;
			foreach (Dictionary<int, ElectricMaterialList.GridParameter> dictionary in this.m_lstDicData)
			{
				num4 += dictionary.Count;
			}
			this.m_grid.Rows = num3 + num4;
			int firstRow = num3;
			foreach (Dictionary<int, ElectricMaterialList.GridParameter> dictionary2 in this.m_lstDicData)
			{
				firstRow = num3;
				foreach (KeyValuePair<int, ElectricMaterialList.GridParameter> keyValuePair in dictionary2)
				{
					ElectricMaterialList.GridParameter value = keyValuePair.Value;
					if (value.LstParm.Count > 0)
					{
						string text = "";
						for (int i = 0; i < value.LstSpec.Count; i++)
						{
							text += value.LstSpec[i];
							if (value.LstSpec.Count > 1)
							{
								text += "\r\n";
							}
						}
						int j = 0;
						while (j < value.LstParm.Count)
						{
							if (j == num)
							{
								string text2 = value.LstParm[j];
								string legendByFamilyName = FamilyLegend.GetLegendByFamilyName(this.m_doc, text2);
								text2 = Path.GetFileName(legendByFamilyName);
								try
								{
									this.m_grid.Images.Add(legendByFamilyName, text2);
									if (this.m_grid.Images.Item(text2) == null)
									{
										goto IL_338;
									}
									this.m_grid.Cell(num3, j + 1).SetImage(text2);
									goto IL_320;
								}
								catch
								{
									goto IL_338;
								}
								goto IL_22A;
							}
							goto IL_22A;
							IL_338:
							j++;
							continue;
							IL_320:
							this.m_grid.Cell(num3, j + 1).Alignment = AlignmentEnum.CenterCenter;
							goto IL_338;
							IL_22A:
							if (j == num2)
							{
								double a = Convert.ToDouble(value.LstParm[j]) * (1.0 + (double)this.m_gridSetInfo.Margin / 100.0);
								string text3;
								if (value.Unit == "m" || value.Unit == "㎡" || value.Unit == "米" || value.Unit == "平方米")
								{
									text3 = a.ToString("0.00");
								}
								else
								{
									text3 = Math.Ceiling(a).ToString("0");
								}
								this.m_grid.Cell(num3, j + 1).Text = text3;
								goto IL_320;
							}
							this.m_grid.Cell(num3, j + 1).Text = value.LstParm[j];
							goto IL_320;
						}
						this.m_grid.Cell(num3, col).Text = text;
						this.m_grid.Cell(num3, col).Alignment = AlignmentEnum.LeftCenter;
						num3++;
					}
				}
				this.m_grid.Range(firstRow, 2, num3 - 1, this.m_grid.Cols - 1).Sort(col2, SortTypeEnum.Text, SortEnum.Ascending);
			}
			this.m_grid.Range(3, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = true;
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontName = this.m_gridSetInfo.FontName;
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontSize = this.m_gridSetInfo.FontSize;
			if (!this.m_gridSetInfo.SetColWidth)
			{
				for (int k = 1; k < this.m_grid.Cols; k++)
				{
					this.m_grid.Column(k).AutoFit();
				}
			}
			for (int l = 1; l < this.m_grid.Rows; l++)
			{
				this.m_grid.Row(l).AutoFit();
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
			return true;
		}

		private bool SetDataToGridDown()
		{
			if (this.m_lstDicData.Count <= 0 || this.m_grid == null)
			{
				YJKMessageBox.Show("没有统计数据");
				return false;
			}
			int num = -1;
			int cols = 0;
			int num2 = 0;
			int col = 0;
			int col2 = 0;
			if (this.m_gridSetInfo.Legend)
			{
				num = 1;
				cols = 8;
				num2 = 5;
				col = 4;
				col2 = 3;
			}
			else
			{
				cols = 7;
				num2 = 4;
				col = 3;
				col2 = 2;
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).DeleteByRow();
			int num3 = 0;
			foreach (Dictionary<int, ElectricMaterialList.GridParameter> dictionary in this.m_lstDicData)
			{
				num3 += dictionary.Count;
			}
			this.m_grid.Rows = num3 + 3;
			this.m_grid.Cols = cols;
			this.DrawTitle(num3);
			int num4 = num3;
			int firstRow = num4;
			foreach (Dictionary<int, ElectricMaterialList.GridParameter> dictionary2 in this.m_lstDicData)
			{
				firstRow = num4;
				foreach (KeyValuePair<int, ElectricMaterialList.GridParameter> keyValuePair in dictionary2)
				{
					ElectricMaterialList.GridParameter value = keyValuePair.Value;
					if (value.LstParm.Count > 0)
					{
						string text = "";
						for (int i = 0; i < value.LstSpec.Count; i++)
						{
							text += value.LstSpec[i];
							if (value.LstSpec.Count > 1)
							{
								text += "\r\n";
							}
						}
						int j = 0;
						while (j < value.LstParm.Count)
						{
							if (j == num)
							{
								string text2 = value.LstParm[j];
								string legendByFamilyName = FamilyLegend.GetLegendByFamilyName(this.m_doc, text2);
								text2 = Path.GetFileName(legendByFamilyName);
								try
								{
									this.m_grid.Images.Add(legendByFamilyName, text2);
									if (this.m_grid.Images.Item(text2) == null)
									{
										goto IL_31D;
									}
									this.m_grid.Cell(num4, j + 1).SetImage(text2);
									goto IL_305;
								}
								catch
								{
									goto IL_31D;
								}
								goto IL_20F;
							}
							goto IL_20F;
							IL_31D:
							j++;
							continue;
							IL_305:
							this.m_grid.Cell(num4, j + 1).Alignment = AlignmentEnum.CenterCenter;
							goto IL_31D;
							IL_20F:
							if (j == num2)
							{
								double a = Convert.ToDouble(value.LstParm[j]) * (1.0 + (double)this.m_gridSetInfo.Margin / 100.0);
								string text3;
								if (value.Unit == "m" || value.Unit == "㎡" || value.Unit == "米" || value.Unit == "平方米")
								{
									text3 = a.ToString("0.00");
								}
								else
								{
									text3 = Math.Ceiling(a).ToString("0");
								}
								this.m_grid.Cell(num4, j + 1).Text = text3;
								goto IL_305;
							}
							this.m_grid.Cell(num4, j + 1).Text = value.LstParm[j];
							goto IL_305;
						}
						this.m_grid.Cell(num4, col).Text = text;
						this.m_grid.Cell(num4, col).WrapText = true;
						this.m_grid.Cell(num4, col).Alignment = AlignmentEnum.LeftTop;
						num4--;
					}
				}
				this.m_grid.Range(firstRow, 2, num4 + 1, this.m_grid.Cols - 1).Sort(col2, SortTypeEnum.Text, SortEnum.Ascending);
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = true;
			this.m_grid.Range(1, 1, num3 + 1, this.m_grid.Cols - 1).FontName = this.m_gridSetInfo.FontName;
			this.m_grid.Range(1, 1, num3 + 1, this.m_grid.Cols - 1).FontSize = this.m_gridSetInfo.FontSize;
			if (!this.m_gridSetInfo.SetColWidth)
			{
				for (int k = 1; k < this.m_grid.Cols; k++)
				{
					this.m_grid.Column(k).AutoFit();
				}
			}
			for (int l = 1; l < this.m_grid.Rows; l++)
			{
				this.m_grid.Row(l).AutoFit();
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
			return true;
		}

		private void DrawTitle(int nRows)
		{
			int num = 1;
			this.m_grid.Cell(nRows + 1, num).Text = "序号";
			if (this.m_gridSetInfo.Legend)
			{
				this.m_grid.Cell(nRows + 1, ++num).Text = "图例";
			}
			this.m_grid.Cell(nRows + 1, ++num).Text = "名称";
			this.m_grid.Cell(nRows + 1, ++num).Text = "规格";
			this.m_grid.Cell(nRows + 1, ++num).Text = "单位";
			this.m_grid.Cell(nRows + 1, ++num).Text = "数量";
			this.m_grid.Cell(nRows + 1, ++num).Text = "备注";
			this.m_grid.Cell(nRows + 2, 1).Text = this.m_strGridName;
			this.m_grid.Range(nRows + 2, 1, nRows + 2, num).Merge();
			this.m_grid.Cell(nRows + 2, 1).FontSize = this.m_gridSetInfo.TitleFontSize;
			this.m_grid.Range(nRows + 1, 1, nRows + 2, num).Alignment = AlignmentEnum.CenterCenter;
		}

		private Document m_doc;

		private List<Dictionary<int, SatatisticDataInfo>> m_lstDicElems;

        private FlexCell.Grid m_grid;

		private List<Dictionary<int, ElectricMaterialList.GridParameter>> m_lstDicData = new List<Dictionary<int, ElectricMaterialList.GridParameter>>();

		private string m_strGridName = "";

		private GridSetInfo m_gridSetInfo;

		private struct GridParameter
		{
			public List<string> LstParm
			{
				get
				{
					return this.m_lstParm;
				}
				set
				{
					this.m_lstParm = value;
				}
			}

			public List<string> LstSpec
			{
				get
				{
					return this.m_lstSepcification;
				}
				set
				{
					this.m_lstSepcification = value;
				}
			}

			public List<string> LstFilter
			{
				get
				{
					return this.m_lstFilter;
				}
				set
				{
					this.m_lstFilter = value;
				}
			}

			public string Unit
			{
				get
				{
					return this.m_strUnit;
				}
				set
				{
					this.m_strUnit = value;
				}
			}

			public GridParameter(List<string> lstOtherParm, List<string> lstSpecParm, List<string> lstFilter, string strUnit)
			{
				this.m_lstParm = lstOtherParm;
				this.m_lstSepcification = lstSpecParm;
				this.m_lstFilter = lstFilter;
				this.m_strUnit = strUnit;
			}

			private List<string> m_lstParm;

			private List<string> m_lstSepcification;

			private List<string> m_lstFilter;

			private string m_strUnit;
		}
	}
}
